colorAdminApp.service('loginService',function() {
   	    
	this.login=function($cookieStore,$rootScope,$scope,$http,$state){
		var messageDic={}
		messageDic["Bad credentials"]="用户名或密码错误";
		messageDic["User account is locked"]="用户被锁定，请联系管理员解锁。";
		messageDic["Maximum sessions of 1 for this principal exceeded"]="该用户已在别处登录，请先退出后再登录，或联系管理员踢除已登录账户。";
		messageDic["User credentials have expired"]="密码已过期。";
		messageDic["User account has expired"]="账号已过期。";
		var entity={};
		entity["username"]=$scope.username;
		entity["password"]=$scope.password;
		//entity["thirdPartyAuth"]="ldap";
		
		$http.post('api/auth/oauth/token?grant_type=password',{params:entity}).success(function(data, status, headers, config) {
		
		//登录成功
		if(data.access_token!=undefined){
			 //将access_token放入cookie中
			 $cookieStore.put("access_token",data.access_token);
			 $state.go("main",{"username":"","photo":""});
		}
		//登录失败
		if(data.access_token==undefined){
			 $scope.showMessage=true;
			 $scope.message=messageDic[data.error_description];
			 if(data.error_description=="User credentials have expired"){
				 $state.go("userPasswordUpdateForExpired",{"username":data.data.username});
			 }
		 }
		 
		 }).error(function(data, status, headers, config) {
	    	
      });  
		
		
	};
	
});